/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package view.contact;

import java.awt.Desktop;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URI;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import model.Contact;
import view.FormEdit;

/**
 * Frame that displays all informations of a Contact
 * @author Gautier
 */
public class LargeDisplay extends JFrame implements ActionListener{
    
    private JPanel cont;
    private JPanel cont2;
    
    private JLabel l_name;
    private JLabel l_address;
    private JLabel l_pPhone;
    private JLabel l_proPhone;
    private JButton b_home;
    private JButton b_email;
    private JLabel l_other;
    
    private JButton edit;
    private JButton delete;
    private JButton close;
    
    private Contact c;
    private ArrayList a;

    /**
     * Constructor
     * @param co Contact object
     * @param al Arraylist of Contacts
     * @throws HeadlessException
     */
    public LargeDisplay(Contact co, ArrayList al) throws HeadlessException {
        
        super(co.getName()+" "+co.getFirstName());
        c=co;
        a=al;
        this.setSize(230, 440);
        this.setDefaultCloseOperation(LargeDisplay.DISPOSE_ON_CLOSE);
        
        // Container
        cont = new JPanel();
        cont.setLayout(new BoxLayout(cont, BoxLayout.Y_AXIS));
        cont2 = new JPanel();
        cont2.setLayout(new BoxLayout(cont2, BoxLayout.X_AXIS));
        
        // Name + FName
        l_name = new JLabel(c.getName()+" "+c.getFirstName());
        cont.add(l_name);
        
        // address
        l_address = new JLabel(c.getHomeAddress());
        cont.add(l_address);
        
        // phone perso
        l_pPhone = new JLabel(c.getPersonalPhone());
        cont.add(l_pPhone);
        
        // pro phone
        l_proPhone = new JLabel(c.getProfessionalPhone());
        cont.add(l_proPhone);
        
        // Home
        b_home = new JButton(c.getHomePage());
        b_home.addActionListener(this);
        cont.add(b_home);
        
        // Email
        b_email = new JButton(c.getEmail());
        b_email.addActionListener(this);
        cont.add(b_email);
        
        // Other
        l_other = new JLabel(c.getOther());
        cont.add(l_other);
        
        //Buttons
        edit = new JButton("Edit");
        edit.addActionListener(this);
        cont2.add(edit);
        delete = new JButton("Delete");
        delete.addActionListener(this);
        cont2.add(delete);
        close = new JButton("Close");
        close.addActionListener(this);
        cont2.add(close);
        cont.add(cont2);
        this.add(cont);
        this.setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==edit){
            new FormEdit(a, c);
            this.dispose();
        }
        if(e.getSource()==delete){ 
            a.remove(c);
            this.dispose();
        }
        if(e.getSource()==close) this.dispose();
        if(e.getSource()==b_email){
            try {
                (Desktop.getDesktop()).mail(new URI("mailto:"+c.getEmail()));
            } catch (Exception ex) {
                Logger.getLogger(LargeDisplay.class.getName()).log(Level.SEVERE, null, ex);
            } 
        }
        if(e.getSource()==b_home){
            try {
                (Desktop.getDesktop()).browse(new URI(c.getHomePage()));
            } catch (Exception ex) {
                Logger.getLogger(LargeDisplay.class.getName()).log(Level.SEVERE, null, ex);
            } 
        }
    }
    
    
}
